USE [CTEM-DB-v1]
GO

/****** Object:  StoredProcedure [dbo].[AllWorker_Update]    Script Date: 02/10/2012 11:29:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Hainh
-- Create date: 06/01/2012
-- Description:	update all workers and theirs Dayworking follow WorkerSalaryID 
-- =============================================
CREATE PROCEDURE [dbo].[AllWorker_Update] 
	             @WorkerSalaryID        BIGINT
AS
BEGIN
	    --UPDATE WORKER--
        UPDATE   [Worker]
        SET      ManDate = 0, 
                 TotalSalary = 0
        WHERE    WorkersSalaryID = @WorkerSalaryID

        --DELETE DAYWORKING
		DECLARE @WORKERID BIGINT
		DECLARE W_CURSOR CURSOR
		FOR SELECT	WorkerID
			FROM	[Worker]
			WHERE	WorkersSalaryID = @WorkerSalaryID
		OPEN	W_CURSOR
		FETCH	NEXT FROM W_CURSOR INTO @WORKERID
		IF		@@FETCH_STATUS <> 0 PRINT 'table Worker HAS NOT worker'
		WHILE	@@FETCH_STATUS = 0
		BEGIN
			--DELETE WH_Storage
			DELETE	DayWorking
			WHERE	WorkerID = @WORKERID
			FETCH	NEXT FROM W_CURSOR INTO @WORKERID
		END
		CLOSE W_CURSOR
		DEALLOCATE W_CURSOR

END

GO

